<?php
namespace app\admin\controller;

class Adminuser extends Base
{
  //欢迎页面
    public function index(){
      $info= [];
      $info["article_count"] = $this->db->query("select count(*) as nums from article")[0];
      $info["user_count"] = $this->db->query("select count(*) as nums from user")[0];
      $info["goods_count"] = $this->db->query("select count(*) as nums from goods")[0];
      $this->fetch(["info" => $info]);
    }
   //编辑
    public function editself($id=''){
        if(is_post()){
            $rule = [
                'password'=>'min:6',
            ];
            $msg = [
                'password.min' => '密码至少6位',
            ];
            $d = $this->post;
            check($rule,$msg,$d);
            if($d['password'] != $d['repassword']){
                err("两次输入密码不一致");
            }
            if(!empty($d['password'])){
                $d['password'] = com_password($d['password']);
            }else{
                unset($d['password']);
            }
            unset($d["repassword"]);
            $this->db->update('admin_user',$d);
            m('Log','xcom')->record($this->userinfo['id'],'修改了自己的资料','admin_user');
            cookie('js_msg','修改成功');
            cookie('js_function','f5');
            return suc();
        }
        $this->fetch(['info'=>$this->userinfo]);
    }
  //列表
  public function lists(){
    if(is_post()){
      $d = $this->post;
      $map = [];
      if($d['keyword']!=''){ $map[] = ['and','title','like',"%{$d['keyword']}%"]; }
      if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
      if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
      $w = where($map);
      $rs = $this->db->query("select *  from admin_user  $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
      $user = $rs[0];
      $w  = where($map);
      //部门信息处理
      $group_list = $this->db->query('select id,title from admin_group');
      foreach($user as $k => $v){
          $user[$k]['group_titles'] = '';
          foreach (explode(",",$v["admin_group_ids"]) as $k1 => $v1) {
            foreach ($group_list as $k2 => $v2) {
              if($v2["id"] == $v1){
                $user[$k]['group_titles'] .= "{$v2["title"]}|";
              }
            }
          }
      }
      if($rs){
        $rule = [
          'create_time' => 'com_time_format',
          'type' => function($v){ return c('type.admin_user_type')[$v];},
        ];
        $data = com_format($user,$rule);
        suc($data,$rs[1]);
      }else{
        suc([],0);
      }
      $this->fetch();
    }
    return $this->fetch();
  }

  //添加
  public function add(){
    if(is_post()){
      $d = $this->post;
      $rule = [
          'username'=>'require|unique:admin_user,username',
          'password'=>'min:6',
      ];
      $msg = [
          'password.min' => '密码至少6位',
      ];
      check($rule,$msg,$d);
      $d['create_time'] = time();
      $d['password'] = com_password($d['password']);
      $d['admin_group_ids'] = isset($d['admin_group_ids']) ? implode(",",$d['admin_group_ids']) : '';

      $id = $this->db->insert('admin_user',$d);
      if ($id) {
        cookie('js_msg','添加成功');
        cookie('js_function','preload');
        return suc();
      }else{
        err('添加失败');
      }
    }
    $info = $this->db->query('select id,title from admin_group');
    return $this->fetch(["info" => $info]);
  }

  //编辑
  public function edit(){
    if(is_post()){
      $d = $this->post;
      $rule = [
          'id' => 'require',
          'username'=>"require|unique:admin_user,username,{$d['id']}",
          'password'=>'min:6',
      ];
      $msg = [
          'password.min' => '密码至少6位',
      ];
      check($rule,$msg,$d);
      $d['update_time'] = time();
      if($d['password']){
          $d['password'] = com_password($d['password']);
      }else{
          unset($d['password']);
      }
      $d['admin_group_ids'] = isset($d['admin_group_ids']) ? implode(",",$d['admin_group_ids']) : '';
      $id = $this->db->update('admin_user',$d);
      if ($id) {
        cookie('js_msg','修改成功');
        cookie('js_function','preload');
        return suc();
      }else{
        err('修改失败');
      }
    }
    $id = $this->get['id'];
    $info = $this->db->query("select * from admin_user where id=?",[$id],1);
    $group_list = $this->db->query('select id,title from admin_group');
    $checked = explode(",",$info["admin_group_ids"]);
    return $this->fetch(['info'=>$info,'group_list'=>$group_list,"checked"=>$checked]);
  }
  //删除
  public function del(){
    if(is_post()){
      $d = $this->post;
      $this->db->delete('admin_user',$d['id']);
      cookie('js_msg','删除成功');
      cookie('js_function','close|reload');
      return suc();
    }
  }

  #批量删除
  function delall(){
    if(is_post()){
      $d = $this->post;
      $ids = $d['id'];
      foreach($ids as $id){
        $this->db->delete('admin_user',$id);
      }
      cookie('js_msg','删除成功');
      cookie('js_function','close|reload');
      return suc();
    }
  }

  //详情
  public function detail($id){
    return $this->fetch();
  }
}
